package route

import (
	"fmt"
	"net/http"
	"pdocker/config"
	"pdocker/task"
	"pdocker/task/build"
	"pdocker/task/lab"
	"pdocker/types"

	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

// @Summary 创建Lab 任务
// @Tags task/lab
// @Description "创建Lab任务所需的所有参数  labDocker中除了image ports cname 都为非必须选项 一般情况下建议不填"
// @Accept json
// @Produce json
// @Param task body lab.TaskCreateModel true "创建Lab任务参数"
// @Success 200 {object} types.TaskResponse "{"code":200,"taskid":"abcd123456"}"
// @Router /task/lab [post]
func LabTaskCreate(c *gin.Context) {
	model := new(lab.TaskCreateModel)
	err := c.BindJSON(model)
	if err != nil {
		log.Warningln(err)
		simpleReturn(c, http.StatusBadRequest, "", "格式错误")
	} else {
		labTask := lab.Task{
			Task: task.Task{
				TaskId:           model.TaskId,
				DestroyTimeStamp: model.DestroyTimeStamp,
				CallBack:         model.CallBack,
			},
			Containers: model.LabDocker,
		}
		var control types.TaskControl = &labTask
		err = control.Create()
		if err != nil {
			errMsg := fmt.Sprintf("创建失败  %s", err)
			log.Error(errMsg)
			simpleReturn(c, http.StatusForbidden, labTask.Task.TaskId, errMsg)
		} else {
			var table types.TaskTable = &lab.TaskTable{Data: labTask}
			ok := table.Insert()
			if ok {
				simpleReturn(c, http.StatusCreated, labTask.Task.TaskId, "创建成功")
				go control.Start()
			} else {
				simpleReturn(c, http.StatusConflict, labTask.Task.TaskId, "任务已存在")
			}

		}
		go control.Start()
	}

}

// @Summary 更新Lab任务
// @Tags task/lab
// @Description "重新指定销毁时间"
// @Accept json
// @Produce json
// @Param task body lab.TaskUpdateModel true "根据销毁时间戳倒计时销毁"
// @Success 200 {object} types.TaskResponse "{"code":200,"taskid":"abcd123456"}"
// @Router /task/lab [put]
func LabTaskUpdate(c *gin.Context) {
	model := new(lab.TaskUpdateModel)
	err := c.BindJSON(model)
	if err != nil {
		simpleReturn(c, http.StatusBadRequest, "", "格式错误")
	} else {
		labTask := lab.Task{
			Task: task.Task{
				TaskId:           model.TaskId,
				DestroyTimeStamp: model.DestroyTimeStamp,
			},
		}
		var control types.TaskControl = &labTask
		var table types.TaskTable = &lab.TaskTable{Data: labTask}
		ok := table.Select()
		if ok {
			go func() {
				table.Update(model.DestroyTimeStamp)
				control.Update(model.DestroyTimeStamp)
			}()
			simpleReturn(c, http.StatusOK, labTask.Task.TaskId, "更新成功")
		} else {
			simpleReturn(c, http.StatusForbidden, labTask.Task.TaskId, "任务不存在")
		}
	}

}

// @Summary 销毁Lab任务
// @tags task/lab
// @Description "立刻销毁Lab任务"
// @Accept json
// @Produce json
// @Param task body lab.TaskDeleteModel true "立即销毁当前任务"
// @Success 200 {object} types.TaskResponse "{"code":200,"taskid":"abcd123456"}"
// @Router /task/lab [delete]
func LabTaskDelete(c *gin.Context) {
	model := new(lab.TaskDeleteModel)
	err := c.BindJSON(model)

	if err != nil {
		simpleReturn(c, http.StatusBadRequest, "", "格式错误")
	} else {
		labTask := lab.Task{
			Task: task.Task{
				TaskId: model.TaskId,
			},
		}
		var table types.TaskTable = &lab.TaskTable{Data: labTask}
		ok := table.Select()

		var control *lab.Task = &labTask
		log.Println(control)
		if ok {
			go func() {
				control.Delete()
				table.Delete()
			}()
			simpleReturn(c, http.StatusOK, labTask.Task.TaskId, "删除成功")
		} else {
			log.Warningf("任务id: %s 不存在\n", labTask.Task.TaskId)
			simpleReturn(c, http.StatusForbidden, labTask.Task.TaskId, "任务不存在")
		}
	}
}

// @Summary 导入实验
// @Tags docker
// @Description "导入实验"
// @Param task body build.BuildModel true "导入实验参数"
// @Produce json
// @Success 200 {object} docker.Stack "{"name":["name1","name2"],id:"sha256:abcd"}"
// @Router /docker/lab/load [post]
func LabLoader(c *gin.Context) {
	model := new(build.LabBuildModel)
	err := c.BindJSON(model)
	if err != nil {
		simpleReturn(c, http.StatusBadRequest, "", "格式错误")
	} else {
		fmt.Println(model)
		go model.Build()
		c.PureJSON(http.StatusOK, model.Filters)
	}

}

// @Summary 导入实验的结果
// @Tags docker
// @Description "构建镜像结果"
// @Param name path string true "查询镜像"
// @Produce json
// @Success 200 {string} string "FROM step 1\n FROM step2"
// @Router /docker/lab/info/{name} [get]
func LabBuilderInformation(c *gin.Context) {
	fmt.Println(config.BuildInformation[c.Param("name")])
	c.String(http.StatusOK, config.BuildInformation[c.Param("name")])
	//todo
}
